package com.lc.servlet;

import com.lc.bean.Grade;
import com.lc.bean.Student;
import com.lc.service.GradeService;
import com.lc.service.StudentService;
import com.lc.service.impl.GradeServiceImpl;
import com.lc.service.impl.StudentServiceImpl;
import com.lc.util.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @description:多请求合一的用于对学生信息进行增删改查等操作的servlet
 * @author: ManolinCoder
 * @time: 2021/8/23
 */
@WebServlet("/Educational/student/studentServlet")
public class StudentServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if ("insert".equals(method)){
            insert(req,resp);
        }else if ("update".equals(method)){
            update(req,resp);
        }else if ("findbyid".equals(method)){
            findbyid(req,resp);
        }else if ("delete".equals(method)){
            deleteStu(req,resp);
        }else {
            findlist(req,resp);
        }

    }

    //删除学生，假删除(更新学生状态state)学生信息的servlet,state为1表示在读，2表示休学，3表示退学，4表示删除
    protected void deleteStu(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String sid = req.getParameter("sid");
        StudentService studentService = new StudentServiceImpl();
        int i = studentService.delStu(sid);
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if (i>0){
            //这里不能直接跳转页面否则页面中会没有数据，应当跳转相应的servlet
            writer.println("<script>alert('删除成功');location.href='/Educational/student/studentServlet';</script>");
        }else {
            writer.println("<script>alert('删除失败');location.href='/Educational/student/studentServlet';</script>");
        }
    }

    //新增学生，添加学生信息
    protected void insert(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取参数
        String stuNo = req.getParameter("stuNo");
        String stuName = req.getParameter("stuName");
        String gid = req.getParameter("gid");
        String sex = req.getParameter("sex");
        String email = req.getParameter("email");
        String phone = req.getParameter("phone");
        String registered = req.getParameter("registered");
        String address = req.getParameter("address");
        String politics = req.getParameter("politics");
        String idNumber = req.getParameter("idNumber");
        String profession = req.getParameter("profession");
        String introduction = req.getParameter("introduction");
        //调取service方法
        StudentService studentService = new StudentServiceImpl();
        //将获取到的参数封装到学生对象中
        Student student = new Student();
        student.setStuNo(stuNo);
        student.setStuName(stuName);
        student.setGid(Integer.parseInt(gid));
        student.setSex(Integer.parseInt(sex));
        student.setEmail(email);
        student.setPhone(phone);
        student.setRegistered(registered);
        student.setAddress(address);
        student.setPolitics(politics);
        student.setIdNumber(idNumber);
        student.setProfession(profession);
        student.setIntroduction(introduction);
        int i = studentService.insertStu(student);
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if (i>0){
            //这里不能直接跳转页面否则页面中会没有数据，应当跳转相应的servlet
            writer.println("<script>alert('新增成功');location.href='/Educational/student/studentServlet';</script>");
        }else {
            writer.println("<script>alert('新增失败');location.href='/Educational/student/getGradeList';</script>");
        }
    }

    //主键查询，根据主键查询学生信息
    protected void findbyid(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String sid = req.getParameter("sid");
        StudentService studentService = new StudentServiceImpl();
        Student student = studentService.findById(Integer.parseInt(sid));
        //查询年级列表
        GradeService gradeService = new GradeServiceImpl();
        List<Grade> list = gradeService.getList();
        req.setAttribute("glist",list);
        req.setAttribute("stu",student);
        req.getRequestDispatcher("edit.jsp").forward(req,resp);
    }

    //查询学生列表，获取学生信息以及处理分页
    protected void findlist(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //引入分页处理类
        PageUtil pageUtil = new PageUtil();
        //1.获取参数
        //1.1模糊查询的条件
        String stuname = req.getParameter("stuname");
        String stuno = req.getParameter("stuno");
        String sex = req.getParameter("sex");
        //1.2分页数据，sql语句中使用limit 开始位置，显示条数
        //需要从页面中获得当前页码值
        String pageIndex = req.getParameter("pageIndex");
        //如果页面没有传入pageIndex的值，则默认查询第一页
        int index = pageIndex==null?1:Integer.parseInt(pageIndex);
        //2.调取service方法
        StudentService studentService = new StudentServiceImpl();
        //这里要判断sex是否为null或者空字符串，否则如果为null或者空字符串的话转换为int类型时会有异常
        int doneSex = (sex==null||sex.length()==0?-1:Integer.parseInt(sex));
        List<Student> students = studentService.getStudents(stuname,stuno,doneSex,index,pageUtil.getPageSize());

        int total = studentService.total(stuname, stuno, doneSex);//总数据条数
        pageUtil.setTotal(total);//有了总数据条数后pageUtil中会自动计算出总页数

        //3.跳转页面
        //如果后台想给前台传数据，是一定要在后台存值的,这里只在跳转后的页面展示就用request域存值即可
        //这里统一先保存到pageUtil里
        pageUtil.setDataList(students);
        pageUtil.setPageIndex(index);
        //存储模糊查询的条件,方便在页面上持续展示
        req.setAttribute("stuname",stuname);
        req.setAttribute("stuno",stuno);
        req.setAttribute("sex",doneSex);
        //存储分页数据
        req.setAttribute("p1",pageUtil);
        //这边list.jsp前不要加/，加/代表从根目录找对应页面，
        //这边我们希望它从servlet指定的路径（Educational/student/studentServlet）直接跳转
        req.getRequestDispatcher("list.jsp").forward(req,resp);
    }

    //修改学生信息，更新学生信息
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取参数
        String stuNo = req.getParameter("stuNo");
        String stuName = req.getParameter("stuName");
        String sid = req.getParameter("sid");
        String gid = req.getParameter("gid");
        String sex = req.getParameter("sex");
        String email = req.getParameter("email");
        String phone = req.getParameter("phone");
        String registered = req.getParameter("registered");
        String address = req.getParameter("address");
        String politics = req.getParameter("politics");
        String idNumber = req.getParameter("idNumber");
        String profession = req.getParameter("profession");
        String introduction = req.getParameter("introduction");
        //调取service方法
        StudentService studentService = new StudentServiceImpl();
        //将获取到的参数封装到学生对象中
        Student student = new Student();
        student.setStuNo(stuNo);
        student.setStuName(stuName);
        student.setGid(Integer.parseInt(gid));
        student.setStuId(Integer.parseInt(sid));
        student.setSex(Integer.parseInt(sex));
        student.setEmail(email);
        student.setPhone(phone);
        student.setRegistered(registered);
        student.setAddress(address);
        student.setPolitics(politics);
        student.setIdNumber(idNumber);
        student.setProfession(profession);
        student.setIntroduction(introduction);
        int i = studentService.updateStu(student);
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if (i>0){
            //这里不能直接跳转页面否则页面中会没有数据，应当跳转相应的servlet
            writer.println("<script>alert('更新成功');location.href='/Educational/student/studentServlet';</script>");
        }else {
            writer.println("<script>alert('更新失败');location.href='/Educational/student/studentServlet?method=findbyid&sid="+sid+"';</script>");
        }
    }
}
